﻿
Imports std
Imports tcpServer3

Public Class rcserve3
    Inherits _rcserve(Of callbackAction, tcpServer3, tcpServer3.talkHandler)

    Private Sub server_receive(ByVal handler As talkHandler) Handles server.receive
        Dim rtns As pointer(Of toBytes()) = Nothing
        Dim invokeAction As callbackAction = Nothing
        Dim types() As Type = Nothing
        If invoke(handler, rtns, invokeAction, types) Then
            Dim end_f As _do(Of Boolean) =
                    Function() As Boolean
                        Dim rtn As nettransData = Nothing
                        rtn = New nettransData()
                        If protocol.write(rtn, +rtns, isemptyarray(types)) Then
                            If Not handler.return(rtn) Then
                                raiseError(errorHandle.errorType.system,
                                           "cannot return to request ", Convert.ToString(handler))
                            End If
                        Else
                            raiseError(errorHandle.errorType.warning,
                                       "cannot write response to return data for request ",
                                       Convert.ToString(handler))
                        End If
                        Return True
                    End Function
            If invokeAction Is Nothing OrElse invokeAction.finished() Then
                'speed up if the function is a synced operation
                'usually we should work in this way, it can save the network connections
                'if an async operation is needed, we should change the protocol
                end_f()
            Else
                Dim action As callbackAction =
                    New callbackAction(Function() As Boolean
                                           Return begin(invokeAction)
                                       End Function,
                                       callbackAction.action_check(invokeAction),
                                       end_f)
                assert_begin(action)
            End If
        End If
    End Sub

    Private Shared Function unittestInvoke(ByVal arg As fromBytes) As bytesInt64
        Dim rtn As bytesInt64 = Nothing
        If istype(Of bytesInt64)(arg) Then
            Return Convert.ToInt64(convertor.cast(Of bytesInt64)(arg))
        Else
            Return rnd(0, strlen(Convert.ToString(arg)), True)
        End If
    End Function

    Private Shared Function unittestInvoke(ByRef args() As fromBytes,
                                           ByRef prtns As pointer(Of toBytes())) As callbackAction
        Dim rtns() As toBytes = Nothing
        ReDim rtns(0)
        rtns(0) = unittestInvoke(args(0))
        If Not prtns Is Nothing Then
            prtns.set(rtns)
        End If
        Return Nothing
    End Function

    Public Shared Sub unittest()
        Dim rcserver As rcserve3 = Nothing
        Try
            raiseError("********** rcserve unitTest **********", errorHandle.errorType.application)

            rcserver = New rcserve3()
            rcserver.register(share.unittest.unittestInvokeName, AddressOf unittestInvoke)
            rcserver.start(share.unittest.port)
            While Not consoleExtSignal()
                sleep()
            End While
            rcserver.stop()

            raiseError("********** finish unitTest ***********", errorHandle.errorType.application)
        Finally
            'for remotecall performance test, the thread will be killed after test finished.
            If Not rcserver Is Nothing Then
                rcserver.stop(0)
                'immediately stop server, as tcp connections cannot be released before app stops.
            End If

            raiseError("********** finish unitTest ***********", errorHandle.errorType.application)
        End Try
    End Sub
End Class
